package example.hello_security.Config;


import example.hello_security.Entity.UserEntity;
import example.hello_security.Mapper.UserMapper;
import example.hello_security.Servlet.IMp.UserServletImp;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.Locale;


@Configuration          //用于标记一个类为配置类，表示该类包含Bean的定义。
@EnableWebSecurity      //其中一个功能：注入SecurityFilterChain
public class WebSecurityConfig{

    // Spring Security 动态JDBC认证
    @Bean
    CustomUserDetailsService customUserDetailsService() {
        return new CustomUserDetailsService();
    }


//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        UserDetails user = (UserDetails)this.users.get(username.toLowerCase(Locale.ROOT));
//        if (user == null) {
//            throw new UsernameNotFoundException(username);
//        } else {
//            return (UserDetails)(user instanceof CredentialsContainer ? user : new User(
//                    user.getUsername(),
//                    user.getPassword(),
//                    user.isEnabled(),
//                    user.isAccountNonExpired(),
//                    user.isCredentialsNonExpired(),
//                    user.isAccountNonLocked(),
//                    user.getAuthorities()));
//        }
//    }



    //基本配置
    //可以通过修改username和password的实参完成自定义登录
//    @Autowired
//    UserServlet userServlet;
//
//    @Resource
//    UserServletImp userServletImp;
//
//    @Bean
//    public UserDetailsService userDetailsService() {
//        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
//        UserEntity user = userServletImp.user("user");  //手动输入实
//
//        manager.createUser(
//                User
//                        .withDefaultPasswordEncoder()
//                        .username(user.getUsername())
//                        .password(user.getPassword())
//                        .build());
//        return manager;
//    }


    //默认配置
//    @Bean
//    @ConditionalOnMissingBean(UserDetailsService.class)
//    InMemoryUserDetailsManager inMemoryUserDetailsManager() {
//        String generatedPassword = "abcdefg123a48721";
//        return new InMemoryUserDetailsManager(
//                User
//                        .withUsername("user")
//                        .password(generatedPassword)
//                        .roles("ROLE_USER")
//                        .build());
//    }
//    @Bean
//    public UserDetailsService users() {
//        // The builder will ensure the passwords are encoded before saving in memory
//        User.UserBuilder users = User.withDefaultPasswordEncoder();
//        UserDetails user = users
//                .username("user")
//                .password("password")
//                .roles("USER")
//                .build();
//        UserDetails admin = users
//                .username("admin")
//                .password("password")
//                .roles("USER", "ADMIN")
//                .build();
//        return new InMemoryUserDetailsManager(user, admin);
//    }
}
